package com.linx.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linx.common.core.domain.model.LoginUser;
import com.linx.common.core.service.OssService;
import com.linx.common.core.utils.MapstructUtils;
import com.linx.common.core.utils.StringUtils;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.satoken.utils.LoginHelper;
import com.linx.system.domain.ProblemFeedback;
import com.linx.system.domain.bo.ProblemFeedbackBo;
import com.linx.system.domain.vo.ProblemFeedbackVo;
import com.linx.system.mapper.ProblemFeedbackMapper;
import com.linx.system.service.IProblemFeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 问题反馈Service业务层处理
 *
 * @author linx
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class ProblemFeedbackServiceImpl implements IProblemFeedbackService {

    private final ProblemFeedbackMapper baseMapper;
    private final OssService ossService;
    /**
     * 查询问题反馈
     *
     * @param problemFeedbackId 主键
     * @return 问题反馈
     */
    @Override
    public ProblemFeedbackVo queryById(Long problemFeedbackId){
        return baseMapper.selectVoById(problemFeedbackId);
    }

    /**
     * 分页查询问题反馈列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 问题反馈分页列表
     */
    @Override
    public TableDataInfo<ProblemFeedbackVo> queryPageList(ProblemFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProblemFeedback> lqw = buildQueryWrapper(bo);
        Page<ProblemFeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的问题反馈列表
     *
     * @param bo 查询条件
     * @return 问题反馈列表
     */
    @Override
    public List<ProblemFeedbackVo> queryList(ProblemFeedbackBo bo) {
        LambdaQueryWrapper<ProblemFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProblemFeedback> buildQueryWrapper(ProblemFeedbackBo bo) {
        LambdaQueryWrapper<ProblemFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProblemDesc()), ProblemFeedback::getProblemDesc, bo.getProblemDesc());
        lqw.eq(bo.getProblemFeedbackStatus() != null, ProblemFeedback::getProblemFeedbackStatus, bo.getProblemFeedbackStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProblemFeedbackReply()), ProblemFeedback::getProblemFeedbackReply, bo.getProblemFeedbackReply());
        lqw.eq(bo.getUserId() != null, ProblemFeedback::getUserId, bo.getUserId());
        lqw.eq(bo.getReplyUserId() != null, ProblemFeedback::getReplyUserId, bo.getReplyUserId());
        return lqw;
    }

    /**
     * 新增问题反馈
     *
     * @param bo 问题反馈
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ProblemFeedbackBo bo) {
        ProblemFeedback add = MapstructUtils.convert(bo, ProblemFeedback.class);
        validEntityBeforeSave(add);
        //反馈人
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setUserId(loginUser.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProblemFeedbackId(add.getProblemFeedbackId());
            if(bo.getOssIdList() != null&&bo.getOssIdList().size()>0){
                ossService.updateBiz(add.getProblemFeedbackId(), ProblemFeedback.class, "problem-feedback",
                    bo.getOssIdList());
            }
        }
        return flag;
    }

    /**
     * 修改问题反馈
     *
     * @param bo 问题反馈
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ProblemFeedbackBo bo) {
        ProblemFeedback update = MapstructUtils.convert(bo, ProblemFeedback.class);

        validEntityBeforeSave(update);
        LoginUser loginUser = LoginHelper.getLoginUser();
        update.setReplyUserId(loginUser.getUserId());
        update.setProblemFeedbackStatus(Long.valueOf(2));
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProblemFeedback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除问题反馈信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
